package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.AgentDao;
import com.woniuxy.entity.Agent;
import com.woniuxy.entity.Transaction;
import com.woniuxy.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Auther: zhujunxin  <br/>
 * Date: 2021/2/24 14:40  <br/>
 * Description:代理的业务层
 */
@Service
public class AgentServiceImpl implements AgentService {

    @Autowired(required = false)
    private AgentDao agentDao;

    @Override
    /**
     * @Description: 通过上级查询下级的信息，包括直属下级和简介下级
     * @Param: [id]
     * @return: java.util.List<com.woniuxy.entity.Agent>
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public List<Agent> selectByReference(int id) {
        List<Agent> newAgent = new ArrayList<>();
        List<Agent> agents = getNewAgent(id, agentDao.selectAll(id), newAgent);
        return agents;
    }

    @Override
    /**
     * @Description: 代理删除其下级代理
     * @Param: [id]
     * @return: void
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public void delete(int id) {
        agentDao.delete(id);
    }

    @Override
    /**
     * @Description: 上级代理更新下级代理的信息
     * @Param: [agent]
     * @return: int
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public int updateById(Agent agent) {
        int i = agentDao.updateById(agent);
        return i;
    }

    @Override
    /**
     * @Description: 查询id对应的上级代理的信息
     * @Param: [id]
     * @return: java.util.List<java.lang.Integer>
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public List<Integer> selectReferenceId(int id) {
        List<Agent> newAgent = new ArrayList<>();
        List<Agent> agents = getNewAgent(agentDao.selectOne(id), agentDao.selectAll(id), newAgent);
        List<Integer> ids = new ArrayList<>();
        for(Agent agent : agents){
            ids.add(agent.getId());
        }
        return ids;
    }

    @Override
    /**
     * @Description: 更新上级代理的总收益
     * @Param: [ids]
     * @return: void
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public void updateTotal(List<Integer> ids) {
        agentDao.updateTotal(ids);
    }

    @Override
    public List<Transaction> selectAllTransaction() {
        return agentDao.selectAllTransaction();
    }

    @Override
    public PageInfo<Transaction> selectTransactionPriceByMemberid(Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page,limit);
        List<Transaction> transactionList=agentDao.selectTransactionPriceByMemberid();
        PageInfo<Transaction> transactionPageInfo = new PageInfo<>(transactionList);
        return transactionPageInfo;
    }

    /**
     * @Description: 在代码中获取当前id对应的上级代理
     * @Param: [agent 当前代理, agents 查询出的所有会员, newAgent 与当前代理有关的代理]
     * @return: java.util.List<com.woniuxy.entity.Agent>
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public List<Agent> getNewAgent(Agent agent, List<Agent> agents, List<Agent> newAgent){
        for(int i = 0; i < agents.size(); i++){
            if(agents.get(i).getId() == agent.getReference()){
                newAgent.add(agents.get(i));
                getNewAgent(agents.get(i),agents,newAgent);
            }
        }
        return newAgent;
    }

    /**
     * @Description: 在代码中查询当前代理的下级代理
     * @Param: [id 当前代理的id, agents 查询出的所有会员, newAgent 与当前代理有关的代理]
     * @return: java.util.List<com.woniuxy.entity.Agent>
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public List<Agent> getNewAgent(int id, List<Agent> agents, List<Agent> newAgent){
        for(int i = 0; i < agents.size(); i++){
            if(agents.get(i).getReference() == id){
                newAgent.add(agents.get(i));
                getNewAgent(agents.get(i).getId(),agents,newAgent);
            }
        }
        return newAgent;
    }
}
